package com.itheima.reggie.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Retention;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 菜品新增功能
     * @param dishDto
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        return R.success("添加菜品成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R selectPage(int page,int pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Dish::getName,name);

        qw.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,qw);

        List<Dish> records = pageInfo.getRecords();//分页查询到的数据

        ArrayList<DishDto> dishDtos = new ArrayList<>();

        for (Dish record : records) {
            Long categoryId = record.getCategoryId();
            Category ca = categoryService.getById(categoryId);
            String categoryName = ca.getName();
            DishDto dishDto = new DishDto();
            dishDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(record,dishDto);
            dishDtos.add(dishDto);
        }

        Page<DishDto> dishDtoPage = new Page<>();

        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        dishDtoPage.setRecords(dishDtos);

        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息,回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getDish(@PathVariable long id){
        DishDto dishDto = dishService.getDishById(id);
        return dishDto!=null? R.success(dishDto): R.error("获取菜品信息失败");
    }


    /**
     * 通过id修改dish表和dishFlavor表中的数据
     * @param dishDto
     * @return
     */
    @PutMapping
    public R updateDish(@RequestBody DishDto dishDto){

        dishService.updateDishFlavor(dishDto);

        return R.success("修改菜品成功");
    }


    /**
     * 删除菜品--物理删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDishs(@RequestParam List<Long> ids){

        dishService.deleteDish(ids);

        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }


        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R getDishById(Long categoryId){

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        qw.eq(Dish::getCategoryId,categoryId);

        List<Dish> list = dishService.list(qw);

        List<DishDto> dishDtos = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtil.copyProperties(dish, dishDto);
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> flavors = dishFlavorService.list(qw2);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtos);
    }
}
